package service

import (
	"gorm.io/gorm"
	"shop-bean/dto"
	"shop-bean/vo"
	"shop-common/library/variables"
	"shop-service/repository"
	"strconv"
	"strings"
)

type IShippingTemplatesRegionService interface {
	FindByIdRegion(tempId uint) ([]*dto.ShippingTemplatesRegionDTO, error)
	BatchDelete(db *gorm.DB, ids []uint) error
}

type ShippingTemplatesRegionService struct {
	db         *gorm.DB
	repository repository.IShippingTemplatesRegionRepository
}

func NewShippingTemplatesRegionService() IShippingTemplatesRegionService {
	return &ShippingTemplatesRegionService{
		db:         variables.GormDB(),
		repository: repository.NewShippingTemplatesRegionRepository(),
	}
}

func (s *ShippingTemplatesRegionService) FindByIdRegion(tempId uint) (list []*dto.ShippingTemplatesRegionDTO, err error) {
	rows, err := s.repository.FindGroup(s.db, tempId)
	if err != nil {
		return nil, err
	}

	for _, obj := range s.concatCity(s.splitGroupConcat(rows)) {
		list = append(list, obj)
	}

	return list, nil
}

//分组切割cityId
func (s *ShippingTemplatesRegionService) splitGroupConcat(rows []*vo.ShippingTemplatesRegionVO) (list []*dto.TempRegion) {
	for _, row := range rows {
		var children []*dto.RegionDTO
		if row.ProvinceId > 0 {
			for _, item := range strings.Split(row.CityIdStr, ",") {
				intNum, _ := strconv.Atoi(item)
				children = append(children, &dto.RegionDTO{
					CityId: uint(intNum),
				})
			}
		}

		list = append(list, &dto.TempRegion{
			UniqId:       row.UniqId,
			ProvinceId:   row.ProvinceId,
			City:         children,
			NameStr:      row.NameStr,
			First:        row.First,
			FirstPrice:   row.FirstPrice,
			Renewal:      row.Renewal,
			RenewalPrice: row.RenewalPrice,
		})
	}

	return list
}

// map去重基本信息
func (s *ShippingTemplatesRegionService) concatCity(rows []*dto.TempRegion) map[string]*dto.ShippingTemplatesRegionDTO {
	var mapDto = make(map[string]*dto.ShippingTemplatesRegionDTO)
	for _, item := range rows {
		if mapDto[item.UniqId] != nil {
			if mapDto[item.UniqId].RegionName == "" {
				mapDto[item.UniqId].RegionName = mapDto[item.UniqId].RegionName + item.NameStr
			} else {
				mapDto[item.UniqId].RegionName = mapDto[item.UniqId].RegionName + ";" + item.NameStr
			}
			mapDto[item.UniqId].Region = append(mapDto[item.UniqId].Region, &dto.RegionDTO{
				CityId:   item.ProvinceId,
				Children: item.City,
			})
		} else {
			mapDto[item.UniqId] = &dto.ShippingTemplatesRegionDTO{
				RegionName:   item.NameStr,
				First:        item.First,
				FirstPrice:   item.FirstPrice,
				Renewal:      item.Renewal,
				RenewalPrice: item.RenewalPrice,
				UniqId:       item.UniqId,
			}

			if item.ProvinceId == 0 {
				mapDto[item.UniqId].RegionName = "默认全国"
				mapDto[item.UniqId].Region = []*dto.RegionDTO{{0, "默认全国", nil}}
			} else {
				mapDto[item.UniqId].Region = []*dto.RegionDTO{{
					CityId:   item.ProvinceId,
					Children: item.City,
				}}
			}
		}
	}

	return mapDto
}

func (s *ShippingTemplatesRegionService) BatchDelete(db *gorm.DB, ids []uint) error {
	return s.repository.BatchDelete(db, ids)
}
